GetSchoolUsersAction   A
last analyzed

Complexity

Total Complexity 2

Size/Duplication

Total Lines 24
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 2
eloc 23
dl 0
loc 24
rs 10
c 0
b 0
f 0

1 Function

Rating   Name   Duplication   Size   Complexity  
A index 0 14 2
1
import { Controller, Inject, UseGuards, Get, Param, NotFoundException } from '@nestjs/common';
2
import { AuthGuard } from '@nestjs/passport';
3
import { ApiTags, ApiBearerAuth, ApiOperation } from '@nestjs/swagger';
4
import { IQueryBus } from 'src/Application/IQueryBus';
5
import { IdDTO } from 'src/Infrastructure/Common/DTO/IdDTO';
6
import { Roles } from 'src/Infrastructure/User/Decorator/Roles';
7
import { RolesGuard } from 'src/Infrastructure/User/Security/RolesGuard';
8
import { UserRole } from 'src/Domain/User/User.entity';
9
import { GetSchoolUsersQuery } from 'src/Application/School/Query/User/GetSchoolUsersQuery';
10
import { SchoolUserView } from 'src/Application/School/View/SchoolUserView';
11
import { GetSchoolVouchersQuery } from 'src/Application/School/Query/Voucher/GetSchoolVouchersQuery';
12
13
@Controller('schools')
14
@ApiTags('School user')
15
@ApiBearerAuth()
16
@UseGuards(AuthGuard('bearer'), RolesGuard)
17
export class GetSchoolUsersAction {
18
  constructor(
19
    @Inject('IQueryBus')
20
    private readonly queryBus: IQueryBus
21
  ) {}
22
23
  @Get(':id/users')
24
  @Roles(UserRole.PHOTOGRAPHER)
25
  @ApiOperation({summary: 'Get users and vouchers for a specific school'})
26
  public async index(@Param() { id }: IdDTO): Promise<SchoolUserView[]> {
27
    try {
28
      const [ users, vouchers ] = await Promise.all([
29
        this.queryBus.execute(new GetSchoolUsersQuery(id)),
30
        this.queryBus.execute(new GetSchoolVouchersQuery(id)),
31
      ]);
32
33
      return users.concat(vouchers);
34
    } catch (e) {
35
      throw new NotFoundException(e.message);
36
    }
37
  }
38
}
39